#include "D3DPlane.h"


D3DPlane::D3DPlane()
{
	m_pVertexBuffer = 0;
	m_pIndexBuffer = 0;
	m_nNumIndicies = 0;
	m_nNumVerts = 0;
}


D3DPlane::~D3DPlane()
{
}

HRESULT D3DPlane::CreatePlane(ID3D11Device* device, float width, float height, const char* texturePath, XMFLOAT4X4 wMatrix)
{
	HRESULT hr = S_OK;
	
	/*if (hr != S_OK)
		return hr;
	
	hr = LoadTexture(device, texturePath);*/
	return hr;
}


HRESULT D3DPlane::LoadTexture(ID3D11Device* device, const char* path)
{
	HRESULT hr = m_pTexture.LoadTexutureToShaderResource(device, path);
	return hr;
}

void D3DPlane::Render(ID3D11DeviceContext* pContext, ShaderGroup* shader, VS_CONSTANT_BUFFER* vsConstBufferInfo, DIR_LIGHT_BUFFER* dlbufferInfo, POINT_LIGHT_BUFFER* plbufferInfo, SPOT_LIGHT_BUFFER* slbufferInfo, float dt)
{
	//Set up constant buffers for vertex shaders
	XMMATRIX matrix = XMLoadFloat4x4(&vsConstBufferInfo->world);

	XMMATRIX rotation = XMMatrixRotationY(dt);

	matrix = XMMatrixMultiply(rotation, matrix);

	XMStoreFloat4x4(&vsConstBufferInfo->world, matrix);

	
	UINT  strides = sizeof(VERTEX);
	UINT  offsets = 0;
	pContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &strides, &offsets);

	shader->SetVShaderInfo(pContext, vsConstBufferInfo);
	shader->SetPShaderInfo(pContext, m_pTexture.GetTexture(), dlbufferInfo, plbufferInfo, slbufferInfo);
	shader->AddShadersToPipeline(pContext);


	pContext->Draw(m_nNumVerts, 0);
}

void D3DPlane::ShutDown()
{
	if (m_pVertexBuffer)
	{
		m_pVertexBuffer->Release();
		m_pVertexBuffer = 0;
	}
	if (m_pIndexBuffer)
	{
		m_pIndexBuffer->Release();
		m_pIndexBuffer = 0;
	}

	m_pTexture.ShutDown();

}

void D3DPlane::SetWorldMatrix(XMFLOAT4X4 newMatrix)
{
	matrix = newMatrix;
}